{include file="common/head"}
<style>
    .hide {
        display: none;
    }
</style>
<form class="layui-form layui-padding-3 layui-form-pane" action="" lat-filter="apply">
    <div class="layui-form-item">
        <label class="layui-form-label">生成卡密 *</label>
        <div class="layui-input-block">
            <input type="number" name="addNum" autocomplete="off" placeholder="请输入卡密生成数量" lay-verify="required"
                class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡密类型 *</label>
        <div class="layui-input-inline">
            <select name="type" id="type">
                <option value="0">积分</option>
                <option value="1">VIP时长</option>
                <option value="2">商品卡密</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item hide" id="goodLis">
        <label class="layui-form-label">对应商品</label>
        <div class="layui-input-inline">
            <select name="good_id" id="type" lay-search="">
                {volist name="goodList" id="good"}
                <option value="{$good.id}">{$good.good_name}</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item hide" id="vip_time">
        <label class="layui-form-label">会员时长 *</label>
        <div class="layui-row">
            <div class="layui-col-xs4">
                <input type="number" name="vip_num" autocomplete="off" placeholder="数量" class="layui-input">
            </div>
            <div class="layui-col-xs4">
                <select name="vip_time_type" id="">
                    <option value="1">天</option>
                    <option value="2">月</option>
                    <option value="3">年</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item" id="score">
        <label class="layui-form-label">积分数量 *</label>
        <div class="layui-input-block">
            <input type="number" name="score" autocomplete="off" placeholder="请输入积分数量" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <button class="layui-btn layui-bg-blue" lay-submit lay-filter="submit">提交</button>
    </div>
</form>

{include file="common/bottom"}
<script>
    layui.use(function () {
        const layer = layui.layer
        const form = layui.form
        $ = layui.$
        form.on('submit(submit)', (data) => {
            var field = data.field;
            if (field.type == 1) {
                delete field.score
                delete field.good_id
            }else if(field.type == 0){
                delete field.vip_time
                delete field.vip_time_type
                delete field.good_id
            }else if(field.type == 2)
            {
                delete field.vip_time
                delete field.vip_time_type
                delete field.score
            }
            $.ajax({
                url: '/admin/fake/doAdd'
                , type: 'post'
                , dataType: 'json'
                , data: field
                , success: (res) => {
                    if (res.code) {
                        layer.msg('操作成功')
                    } else {
                        layer.msg(res.message, { icon: 5 })
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        })
        $('#type ~ .layui-form-select > .layui-anim-upbit > dd').click(() => {
            $('#goodLis').hide()
            $('#score').hide()
            $('#vip_time').hide()
            setTimeout(() => {
                let type = $('#type').val()
                if (type == 2) {
                    $('#goodLis').show()
                } else if (type == 0) {
                    $('#score').show()
                }else if(type == 1)
                {
                    $('#vip_time').show()
                }
            }, 200)
        })
    })

</script>